package com.dq.usercenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.dq.commonutils.dto.OrderUserVo;
import com.dq.commonutils.jwt.JwtUtil;
import com.dq.commonutils.pojo.DqException;
import com.dq.usercenter.entity.UcenterMember;
import com.dq.usercenter.mapper.UcenterMemberMapper;
import com.dq.usercenter.service.UcenterInfoMemberService;
import com.dq.usercenter.vo.UserMemberVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @project: guli_parent
 * @ClassName: UcenterInfoMemberServiceImpl
 * @author: dq
 * @creat: 2022/8/7 18:19
 */
@Slf4j
@Service
public class UcenterInfoMemberServiceImpl implements UcenterInfoMemberService {
    @Autowired
    private UcenterMemberMapper userMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public UserMemberVo getUserInfo(String token) {
        /**
         * 1. 校验数据
         *  1.1 判断是否为空
         *  1.2 判断token是否有效
         *  1.3 返回解析的数据（如果有数据则返回 如果Redis无数据则返回null）
         *
         * 2。 根据上面的返回判断
         *  2.1 如果Vo类返回的为空 则说明Redis到期了 则直接返回一个error
         *  2.2 如果不为空则去查询数据库的数据封装为VO对象返回
         */

        //获取redis中的数据
        UserMemberVo userMemberVo = checkTokenByIdAndFindUserInfoByRedis(token);
        if (userMemberVo ==null){
            throw new DqException(20003,"用户信息过期，请从新登录");
        }

        return userMemberVo;
    }

    //远程order调用
    @Override
    public OrderUserVo getOrderUserInfo(String userId) {
        UcenterMember ucenterMember = userMapper.selectById(userId);
        OrderUserVo orderUserVo = new OrderUserVo();
        BeanUtils.copyProperties(ucenterMember,orderUserVo);
        return orderUserVo;
    }

    //远程statistics调用 用于获取一天的总注册人数
    @Override
    public Integer findTotalRegisterCountFromOneDay(String day) {
        int count = userMapper.findTotalRegisterCountFromOneDay(day);
        return count;
    }


    /**
     * 根据token校验最终从redis获取数据返回
     * @param token
     * @return  null  Or  UserMemberVo
     */
    private UserMemberVo checkTokenByIdAndFindUserInfoByRedis(String token) {
        if (StringUtils.isEmpty(token)){
            throw new DqException(20001,"校验异常");
        }
        Map<String, Object> map = JwtUtil.checkToken(token);
        if (map==null){
            throw new DqException(20001,"校验异常");
        }
        String userId = (String) map.get("userId");
        //获取Redis数据  判断是否到期
        log.info("------------------------------");
        String userInfo = redisTemplate.opsForValue().get("TOKEN-" + token);
        log.info("走了Redis 获取到了用户基本信息  数据为 : {}",userInfo);
        log.info("直接返回前端Object对象");
        log.info("------------------------------");
        if (StringUtils.isEmpty(userInfo)){
            return null;
        }
        UserMemberVo userMemberVo  = JSON.parseObject(userInfo, UserMemberVo.class);
        return userMemberVo;
    }
}
